#include "StdAfx.h"
#include "cln_login_handler.h"

namespace fly {

	namespace jld{

		namespace client {	

			cln_login_handler::cln_login_handler(bool is_test)
				:cln_handler(is_test)
			{
			}

			bool cln_login_handler::handle_send(recv_buf& recv_buf, send_buf& send_buf, cln_info& info)
			{

				if ((!is_test_) || (!info.account().empty()))
				{
					return cln_handler::handle_send(recv_buf, send_buf, info);
				}
				LOG_DEBUG("Test user login handler");

				User user;
				user.set_action(LOGIN);
				string account = "jack";
				Sleep(1);
				account.append(boost::lexical_cast<string>(timestamp->cur_mill_sec()));
				user.set_account(account);
				user.set_password("123456");
				info.set_account(account);
				return send_buf.serialize_to_send(USER, target_, user);
			}

			bool cln_login_handler::handle_recv(recv_buf& recv_buf, send_buf& send_buf, cln_info& info)
			{
				if (USER != recv_buf.pid())
				{
					return cln_handler::handle_recv(recv_buf, send_buf, info);
				}
				return true;
			}
			bool cln_login_handler::handle_ack(Ack& ack, send_buf& send_buf, cln_info& info)
			{
				if (USER != ack.what() || LOGIN != ack.action())
				{
					return cln_handler::handle_ack(ack, send_buf, info);
				}
				LOG_INFO("User ack");
				if (!ack.success())
				{
					EXPECT_EQ("Account or password mistake", ack.msg());
				}
				return true;
			}

		}
	}
}